//
//  NSMAssetBrowserView.m
//  BSProject
//
//  Created by nsm on 2019/11/25.
//  Copyright © 2019 beisheng. All rights reserved.
//

#import "NSMAssetBrowserView.h"

@interface NSMAssetBrowserCollectionViewCell : UICollectionViewCell
@property (nonatomic, strong) NSMAssetBrowserCell *browseView;
@end

@implementation NSMAssetBrowserCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _browseView = NSMAssetBrowserCell.new;
        [self.contentView addSubview:_browseView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (!CGRectEqualToRect(self.browseView.frame, self.contentView.bounds)) {
        self.browseView.frame = self.contentView.bounds;
    }
}
@end

static NSString * const kUnknownCellIdentifier = @"asset_unknown_cell_identifier";

@interface NSMAssetBrowserView () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, NSMAssetBrowserZoomDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *collectionViewLayout;
@end

@implementation NSMAssetBrowserView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self didInitialize];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self didInitialize];
    }
    return self;
}

- (void)didInitialize {
    _collectionViewLayout = UICollectionViewFlowLayout.new;
    _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds
                                         collectionViewLayout:_collectionViewLayout];
    _collectionView.backgroundColor = UIColor.clearColor;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsHorizontalScrollIndicator = YES;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.scrollsToTop = NO;
    // 让subview立即响应touch事件
    _collectionView.delaysContentTouches = NO;
    _collectionView.decelerationRate = UIScrollViewDecelerationRateFast;
    if (@available(iOS 11, *)) {
        _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [_collectionView registerClass:NSMAssetBrowserCollectionViewCell.class forCellWithReuseIdentifier:kUnknownCellIdentifier];
    [self addSubview:_collectionView];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (!CGRectEqualToRect(self.collectionView.frame, self.bounds)) {
        [self.collectionViewLayout invalidateLayout];
        self.collectionView.frame = self.bounds;
    }
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.delegate numberOfItemsInAssetBrowserView:self];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMAssetBrowserCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kUnknownCellIdentifier forIndexPath:indexPath];
    cell.browseView.delegate = self;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMAssetBrowserCollectionViewCell *tempCell = (NSMAssetBrowserCollectionViewCell *)cell;
    [tempCell.browseView viewWillAppear];
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSMAssetBrowserCollectionViewCell *tempCell = (NSMAssetBrowserCollectionViewCell *)cell;
    [tempCell.browseView viewDidDisappear];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return collectionView.bounds.size;
}

#pragma mark - NSMAssetBrowserZoomDelegate
- (void)singleTapInCell:(NSMAssetBrowserCell *)cell location:(CGPoint)location {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate singleTapInCell:cell location:location];
    }
}

- (void)doubleTapInCell:(NSMAssetBrowserCell *)cell location:(CGPoint)location {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate doubleTapInCell:cell location:location];
    }
}

- (void)longPressInCell:(NSMAssetBrowserCell *)cell {
    if ([self.delegate respondsToSelector:_cmd]) {
        [self.delegate longPressInCell:cell];
    }
}

@end
